package com.young.ums.task;

import com.young.common.core.concurrent.AbstractTask;
import com.young.common.core.event.EventHelper;
import com.young.interfaces.common.publisher.ECommonEventPublisherEnum;
import com.young.interfaces.ums.model.event.EUmsEventDefinitionEnum;
import com.young.interfaces.ums.model.event.LoginSuccessEvent;
import com.young.ums.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 登录成功异步事件发布
 *
 * @author wangjy
 * @date 2021/11/13 14:20:11
 */
public class SendLoginSuccessEventTask extends AbstractTask {

    private static final Logger logger = LoggerFactory.getLogger(SendLoginSuccessEventTask.class);

    /**
     * 登录用户
     */
    private User user;

    public SendLoginSuccessEventTask(User user) {
        this.user = user;
    }

    /**
     * 执行业务
     */
    @Override
    public void exec() {
        try {
            LoginSuccessEvent event = new LoginSuccessEvent();
            event.setUserId(user.getId());
            event.setUserName(user.getName());
            event.setUserCreateTime(user.getCreateTime());
            EventHelper.publish(EUmsEventDefinitionEnum.LOGIN_SUCCESS, ECommonEventPublisherEnum.UMS, event);
        } catch (Throwable t) {
            logger.error("[异步发布登录成功事件] 发生异常,登录用户={}", user, t);
        }
    }
}
